package structure;

/**
 * 通过链表实现队列
 */
public class LinkQueue {

    private Node front;
    private Node rear;
    private int size;

    public LinkQueue() {
        this.front = new Node();
        this.rear = new Node();
    }

    /**
     * 入队
     * @param value
     */
    public void in(Object value)throws  Exception{
      Node newNode = new Node(value);
      Node temp = front;

      while (temp.next != null){
          temp = temp.next;
      }

      temp.next = newNode;
      rear = newNode;
      size ++;
    }

    /**
     * 出队
     * @throws Exception
     */
    public Object out()throws  Exception{
        if(front.next == null){
            throw new  Exception("队列为空异常");
        }
        Node firstNode = front.next;
        front.next = firstNode.next;
        size--;

        return firstNode.data;
    }

    /**
     * 遍历队列
     */
    public void traverse(){
        Node temp = front.next;
        while ( temp != null){
            System.out.println(""+temp.data);

            temp = temp.next;
        }
    }
}
